﻿using System;
using System.Collections.Generic;
using System.Text;
using AMS.DataAccess.Sys.BO;
using AMS.DataAccess.Common;
using AMS.DataAccess.Sql;
using AMS.DataAccess.Sys.Interfaces;

namespace AMS.DataAccess.Sys
{
    public class RoleManager : IRoleManager
    {
        public List<RoleInfo> GetAllRoles(CallContext context)
        {
            string sql = "SELECT * FROM TS_Role ORDER BY RoleName ASC";
            var ds = SqlDBHelper.Query(sql);
            return BusinessObjectMapping.DataTableToBOList<RoleInfo>(ds.Tables[0]);
        }

        public void AddRole(CallContext context, RoleInfo role)
        {
            SqlInsert insert = new SqlInsert("TS_Role");
            insert.AddBusinessObject(role);
            SqlBuilder sql = new SqlBuilder();
            sql.Add(insert.ToString());
            sql.Add("SELECT @@IDENTITY");
#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(sql.ToString());
#endif
            var ds = SqlDBHelper.Query(sql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                role.AID = AdoConverter.ToInt32(ds.Tables[0].Rows[0][0]);
            }
        }

        public bool DeleteRole(CallContext context, int roleId)
        {
            SqlDelete delete = new SqlDelete("TS_Role");
            delete.AddCondition(new SqlCondition("AID", roleId));
#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(delete.ToString());
#endif
            return 0 < SqlDBHelper.ExecuteSql(delete.ToString());
        }

        public List<FunctionInfo> GetPrivilages(CallContext context, int roleId)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT * FROM TS_Privilage p");
            sql.Add("INNER JOIN TS_Function f ON p.FunctionID = f.AID");
            sql.Add("WHERE p.RoleID = 2");
#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(sql.ToString());
#endif
            var ds = SqlDBHelper.Query(sql.ToString());
            return BusinessObjectMapping.DataTableToBOList<FunctionInfo>(ds.Tables[0]);
        }

        public List<FunctionInfo> GetPrivilagesByModule(CallContext context, int roleId, int moduleId)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT p.RoleID, t.*");
            sql.Add("FROM TS_Privilage p");
            sql.Add("INNER JOIN (");
            sql.Add("    SELECT * FROM TS_Function");
            sql.Add("    WHERE ", new SqlCondition("ModuleID", moduleId));
            sql.Add("    )t ON p.FunctionID = t.AID");
            sql.Add("WHERE ", new SqlCondition("RoleID", roleId));
            sql.Add("ORDER BY t.FunctionName ASC");
#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(sql.ToString());
#endif
            var ds = SqlDBHelper.Query(sql.ToString());
            return BusinessObjectMapping.DataTableToBOList<FunctionInfo>(ds.Tables[0]);
        }

        public int BatchUpdatePrivilage(CallContext context, int roleId, List<int> addedFunctions, List<int> deletedFunctions)
        {
            SqlBatch batch = new SqlBatch();
            List<int> allIds = new List<int>();
            allIds.AddRange(deletedFunctions);
            allIds.AddRange(addedFunctions);

            SqlDelete delete = batch.CreateSqlDelete("TS_Privilage");
            delete.AddCondition(new SqlCondition("RoleID", roleId));
            SqlConditionList conditionList = new SqlConditionList("FunctionID", allIds);
            delete.AddCondition(conditionList.ToString());

            foreach (int id in addedFunctions)
            {
                SqlInsert insert = batch.CreateSqlInsert("TS_Privilage");
                insert.AddColumn("RoleID", roleId);
                insert.AddColumn("FunctionID", id);
            }
#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(batch.ToString());
#endif
            return SqlDBHelper.ExecuteSql(batch.ToString());
        }

        public int BatchAddRoleUsers(CallContext context, int roleId, List<int> addedUser)
        {
            return BatchUpdateRoleUsers(context, roleId, addedUser, null);
        }

        public int BatchDeleteRoleUsers(CallContext context, int roleId, List<int> deletedUsers)
        {
            return BatchUpdateRoleUsers(context, roleId, null, deletedUsers);
        }

        public int BatchUpdateRoleUsers(CallContext context, int roleId, List<int> addedUsers, List<int> deletedUsers)
        {
            SqlBatch batch = new SqlBatch();
            List<int> allIds = new List<int>();
            if (deletedUsers != null)
            {
                allIds.AddRange(deletedUsers);
            }
            if (addedUsers != null)
            {
                allIds.AddRange(addedUsers);
            }

            SqlDelete delete = batch.CreateSqlDelete("TS_UserRole");
            delete.AddCondition(new SqlCondition("RoleID", roleId));
            SqlConditionList conditionList = new SqlConditionList("UserID", allIds);
            delete.AddCondition(conditionList.ToString());

            if (addedUsers != null)
            {
                foreach (int id in addedUsers)
                {
                    SqlInsert insert = batch.CreateSqlInsert("TS_UserRole");
                    insert.AddColumn("RoleID", roleId);
                    insert.AddColumn("UserID", id);
                }
            }
#if SHOW_SQL
            System.Diagnostics.Debug.WriteLine(batch.ToString());
#endif
            return SqlDBHelper.ExecuteSql(batch.ToString());
        }
    }
}
